package com.jsyl.lwbk.web.controller.business;

import com.jsyl.lwbk.business.domain.Child;
import com.jsyl.lwbk.business.dto.ChildDTO;
import com.jsyl.lwbk.business.service.IChildService;
import com.jsyl.lwbk.business.vo.ChildVO;
import com.jsyl.lwbk.common.annotation.Log;
import com.jsyl.lwbk.common.constant.Constants;
import com.jsyl.lwbk.common.constant.UserConstants;
import com.jsyl.lwbk.common.core.controller.BaseController;
import com.jsyl.lwbk.common.core.domain.AjaxResult;
import com.jsyl.lwbk.common.core.page.TableDataInfo;
import com.jsyl.lwbk.common.enums.BusinessType;
import com.jsyl.lwbk.common.utils.StringUtils;
import com.jsyl.lwbk.framework.util.ShiroUtils;
import com.jsyl.lwbk.system.domain.SysRole;
import com.jsyl.lwbk.system.domain.SysUser;
import com.jsyl.lwbk.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Api(value = "学生")
@Controller
@RequestMapping("/business/child")
public class ChildController extends BaseController {

    private String prefix = "business/childEvaluate";

    @Autowired
    IChildService childService;

    @Autowired
    ISysUserService sysUserService;

    @RequiresPermissions("business:child:view")
    @GetMapping()
    public String child(ModelMap mmap) {
        return prefix + "/child";
    }

    @ApiOperation(value = "学生list")
    @ApiImplicitParams({@ApiImplicitParam(name = "centerId", value = "中心id"),
            @ApiImplicitParam(name = "childName", value = "学生姓名"),
            @ApiImplicitParam(name = "telephone", value = "电话"),
            @ApiImplicitParam(name = "beginAge", value = "开始年龄"),
            @ApiImplicitParam(name = "endAge", value = "结束年龄"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "memberType", value = "会员类型")
    })
    @RequiresPermissions("business:child:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo findChildList(ChildDTO childDTO) {
        Long userId = ShiroUtils.getUserId();
        List<String> roleKeyList = new ArrayList<String>();
        if(!CollectionUtils.isEmpty(ShiroUtils.getSysUser().getRoles())) {
            List<SysRole> sysRoles = ShiroUtils.getSysUser().getRoles();
            for(SysRole sysRole:sysRoles){
                roleKeyList.add(sysRole.getRoleKey());
            }
        }
        startPage();
        List<ChildVO> list = childService.findChildList(childDTO, roleKeyList, userId);
        return getDataTable(list);
    }


    /**
     * 新增用户
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        return prefix + "/add";
    }

    /**
     * 新增保存用户
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Child child) {
        child.setCreateUser(ShiroUtils.getLoginName());
        this.childService.insertChild(child);
        return  success();
    }

    /**
     * 修改用户
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("child", childService.findById(id));
        return prefix + "/edit";
    }

    /**
     * 修改保存用户
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Child child) {
        return toAjax(childService.updateChild(child));
    }

}
